import { _decorator, Component, game, Label, Node, Sprite, sys } from 'cc';
import { HttpCom } from './net/HttpCom';
import { GlobalVar } from './GlobalVar';
import { PayEvent } from './EventName';
const { ccclass, property } = _decorator;

@ccclass('StoreItem')
export class StoreItem extends Component {

    @property(Sprite)
    icon: Sprite = null;
    @property(Node)
    first: Node = null;
    @property(Label)
    countLab: Label = null;
    @property(Node)
    giveMore: Node = null;
    @property(Label)
    priceLab: Label = null;

    storeId: number = 0;
    start() {
        this.first.active = false;
        this.giveMore.active = false;

    }

    initView(count: string, price: string,id:number){
        this.storeId = id;
        this.countLab.string = count;
        this.priceLab.string = price;
    }

    onBuyBtnClick(){
        
        HttpCom.getReceiveAddress((resp)=>{
            GlobalVar.receiveAddress = resp.data.receiveAddress;
            HttpCom.buy((resp1)=>{
                let data = {
                    userId: GlobalVar.userInfo.id,
                    storeId: this.storeId,
                    orderNo: resp1.data.orderNo
                }
                let jsonData = JSON.stringify(data);
                //let payUrl = "ton://transfer/" + GlobalVar.receiveAddress + "?amount=" + this.priceLab.string + "&text=" + jsonData;
               // sys.openURL(payUrl);
               let price = parseFloat(this.priceLab.string);
                game.emit(PayEvent.TRANSFER_TON,this.priceLab.string,jsonData);
            });
        })
    }

    update(deltaTime: number) {
        
    }
}


